package com.ayuan.blog.service.impl;

import com.github.pagehelper.PageInfo;
import org.apache.commons.collections.CollectionUtils;
import org.durcframework.core.expression.ExpressionQuery;
import org.durcframework.core.expression.subexpression.ValueExpression;
import org.durcframework.core.service.CrudService;
import com.ayuan.blog.service.FocusService;
import com.ayuan.blog.dao.FocusDao;
import com.ayuan.blog.domain.Focus;
import com.ayuan.blog.domain.sch.FocusSch;
import org.springframework.stereotype.Service;

import java.util.List;
    
@Service
public class FocusServiceImpl 
        extends CrudService<Focus, FocusDao> 
        implements FocusService {

    @Override
    public PageInfo<Focus> findPage(FocusSch sch) {
        ExpressionQuery query = new ExpressionQuery();
        query.addPaginationInfo(sch);
        query.addAnnotionExpression(sch);

        long total = this.getDao().findTotalCount(query);
        List<Focus> list = this.getDao().find(query);

        PageInfo<Focus> page = new PageInfo<Focus>();
        page.setList(list);
        page.setPageNum(sch.getPageIndex()); // 设置当前页数
        page.setPageSize(sch.getPageSize()); // 设置每页的数量
        page.setSize(list.size()); // 设置当前页的数量
        page.setPages((int) ((total + sch.getPageSize() - 1) / sch.getPageSize())); // 设置总的页数
        page.setTotal(total); // 设置总的数量

        return page;
    }

    @Override
    public Focus getByUserIdAndFocusedUserId(Long userId, Long focusedUserId) {
        ExpressionQuery query = new ExpressionQuery();
        query.addValueExpression(new ValueExpression("user_id", userId));
        query.addValueExpression(new ValueExpression("focused_user_id", focusedUserId));

        List<Focus> focusList = find(query);

        if (CollectionUtils.isNotEmpty(focusList)) {
            return focusList.get(0);
        }

        return null;
    }

    @Override
    public List<Focus> getListByUserId(Long userId) {
        ExpressionQuery query = new ExpressionQuery();
        query.addValueExpression(new ValueExpression("user_id", userId));

        return find(query);
    }
}